package com.beta.admin.controller;

import com.baomidou.mybatisplus.plugins.Page;
import com.beta.admin.request.home.HomeBannerCreateRequest;
import com.beta.admin.request.home.HomeBannerUpdateRequest;
import com.beta.admin.support.web.BaseController;
import com.beta.common.web.ContentType;
import com.beta.dto.home.HomeBannersDto;
import com.beta.service.IHomeBannersService;
import com.beta.vo.home.HomeBannersVO;
import io.swagger.annotations.Api;
import ma.glasnost.orika.MapperFacade;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * Created by vector on 2017/2/15.
 */
@Api(tags = "HomeBanners")
@RestController
@RequestMapping(value = RequestMappingConstants.MAPPING_PREFIX + "/home_banners")
public class HomeBannerController extends BaseController {

    @Autowired
    private IHomeBannersService homeBannersService;

    @Autowired
    private MapperFacade beanMapper;

    @RequiresPermissions("home_banners:list")
    @GetMapping(value = "", produces = {ContentType.JSON})
    public Object homeBanners() {
        Page<HomeBannersDto> homeBannersDtoPage = homeBannersService.getHomeBannersPage(getMybatisPage());
        return pageData(homeBannersDtoPage, homeBannersDto -> beanMapper.map(homeBannersDto, HomeBannersVO.class));
    }

    @RequiresPermissions("home_banners:new")
    @PostMapping(value = "", produces = {ContentType.JSON})
    public Object createHomeBanner(@Valid @RequestBody HomeBannerCreateRequest createRequest) {
        homeBannersService.createHomeBanner(beanMapper.map(createRequest, HomeBannersDto.class));
        return ok();
    }

    @RequiresPermissions("home_banners:view")
    @GetMapping(value = "/{id}", produces = {ContentType.JSON})
    public Object getHomeBannerById(@PathVariable("id") Integer id) {
        HomeBannersDto homeBanner = homeBannersService.getHomeBannerById(id);
        return ok(beanMapper.map(homeBanner, HomeBannersVO.class));
    }

    @RequiresPermissions("home_banners:update")
    @PatchMapping(value = "/{id}", produces = {ContentType.JSON})
    public Object updateHomeBannerById(@PathVariable("id") Integer id,
                                       @Valid @RequestBody HomeBannerUpdateRequest updateRequest) {
        homeBannersService.updateHomeBannerById(id, beanMapper.map(updateRequest, HomeBannersDto.class));
        return ok();
    }

    @RequiresPermissions("home_banners:delete")
    @DeleteMapping(value = "/{id}", produces = ContentType.JSON)
    public Object deleteHomeBannerById(@PathVariable("id") Integer id) {
        homeBannersService.deleteById(id);
        return ok();
    }

}
